/*
 * rrc_asn_decoder.c
 *
 *  Created on: Apr 13, 2018
 *      Author: root
 */
#ifdef	HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>	/* for atoi(3) */
#include <unistd.h>	/* for getopt(3) */
#include <string.h>	/* for strerror(3) */
#include <sysexits.h>	/* for EX_* exit codes */
#include <errno.h>	/* for errno */

#include "asn_application.h"
#include "../libs/a36331-c50.h"
#include "asn_internal.h"
#include "BCCH-DL-SCH-Message.h"
#include "SystemInformationBlockType1.h"
#include "BIT_STRING.h"
#include "UL-DCCH-Message.h"
#include "per_decoder.h"
#include "rrc_asn_decoder.h"
//#define PDU BCCH_BCH_Message
//#define ASN_PDU_COLLECTION


extern asn_TYPE_descriptor_t *asn_pdu_collection[];

sib1 g_sib1;
sib2 g_sib2;
sib3 g_sib3;
sib5 g_sib5;

void StrToHex(unsigned char *pbDest, char *pbSrc, int nLen)
{
char h1,h2;
char s1,s2;
int i;

for (i=0; i<nLen; i++)
{
	if(pbSrc[2*i]>='a'&& pbSrc[2*i]<='z')
	{
		pbSrc[2*i]-='a'-'A';
	}
	if(pbSrc[2*i+1]>='a'&& pbSrc[2*i+1]<='z')
	{
		pbSrc[2*i+1]-='a'-'A';
	}
	h1 = pbSrc[2*i];
	h2 = pbSrc[2*i+1];

	s1 = h1 - 0x30;
	if (s1 > 9)
	s1 -= 7;

	s2 = h2 - 0x30;
	if (s2 > 9)
	s2 -= 7;

	pbDest[i] = s1*16 + s2;
	}
}

void main(int argc ,char *argv[])
{
//	char sibPayload[] = "60518023bafc000d43193cea00481040c0";
//	unsigned char sibPayload[] = {0x60,0x51,0x80,0x23,0xba,0xfc,0x00,0x0d,0x43,0x19,0x3c,0xea,0x00,0x48,0x10,0x40,0xc0}; //sib1
//	unsigned char sibPayload[] = {0x00,0x01,0x03,0xEC,0x07,0xED,0xFB,0x24,0x01,0x8C,0x80,0xD5,0x14,0x08,0x00,0x00,0x40,0x04,0x21,0x78,0xAD,0x5C,0x26,0xE6,0x14,0x0E}; //sib2
//	unsigned char sibPayload[] = {0x00,0x04,0x3D,0x9F,0xF8,0x0D,0x47,0x69,0x00};//sib3
//	unsigned char sibPayload[] = {0x00,0x0D,0x84,0x03,0x39,0x0C,0x56,0xBA,0xC8,0x00,0x11,0x41,0x8A,0xD7,0x08,0x00,0x14,0x61,0x8A,
//									0xD7,0x08,0x00,0x15,0xF1,0x8A,0xD7,0x08,0x00,0x17,0x81,0x8A,0xD7,0x08,0x00,0x1C,0x31,
//									0x8A,0xD7,0x48,0x00,0x5D,0xC1,0x8A,0xD6,0xC8,0x00,0x00,0x00,0x00}; // sib5
//	unsigned char sibPayload[] = {0x00,0x80,0x1c,0x31,0x18,0x6f,0xe1,0x28,0xe8,0x30,0x8c,0x96,0xe3,0x40,0x00,0x08,0x03,0x66,0x02,0x4f,0x6c,0xb5,
//			0x02,0xcb,0xc0,0x01,0x83,0x25,0x84,0xea,0x45,0x49,0xe0,0x28,0x27,0x54,0xac,0x24,0x00,0x00,0x00};

//	unsigned char sibPayload[] = {00,0x01,0x03,0xec,0x07,0xed,0xfb,0x24,0x01,0x88,0x80,0xd5,0x14,0x08,0x00,0x00,0x40,0x04,0x21,0x78,0xad,0x5c,0x26,0xe6,0x14,0x0e};
	if(2 != argc)
	{
		printf("args is error\n");
		return;
	}
	unsigned char db[100] = {0};
	int sibPayloadLength = strlen(argv[1])/2;
	printf("sibPayloadLength %d \n",sibPayloadLength);
	StrToHex(db,argv[1],sibPayloadLength);
	int k = 0;

	for(k = 0;k< sibPayloadLength;k++)
	{
		printf("%02x ",db[k]);
	}
	printf("\n");




	asn_dec_rval_t rval_e;
	asn_codec_ctx_t ctx_e;
	BCCH_DL_SCH_Message_t * R12_BCCH_DL_SCH_Message_Ptr = NULL;
//	SystemInformationBlockType1_t* SystemInformationBlockType1_Ptr = NULL;
	ctx_e.max_stack_size = 0;
	rval_e = uper_decode_complete(&ctx_e,asn_pdu_collection[1],
			(void **)&R12_BCCH_DL_SCH_Message_Ptr,db,sibPayloadLength);
	int i = 0;
	int j = 0;
	int8_t present = -1;

	if(RC_OK == rval_e.code)
	{
		sleep(1);
		printf("BCCH_DL_SCH_Message decoder successful \n");
		printf("present %d\n",R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.present);
		if(BCCH_DL_SCH_MessageType__c1_PR_systemInformationBlockType1 ==
				R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.present)
		{
			fill_sib1_context(&(R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformationBlockType1));
		}else
		{
			if(BCCH_DL_SCH_MessageType__c1_PR_systemInformation
					== R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.present)
			{
				for(i = 0;
						i < R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformation.criticalExtensions.choice.systemInformation_r8.sib_TypeAndInfo.list.count;
						i++)
				{
					present = R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformation.criticalExtensions.choice.systemInformation_r8.sib_TypeAndInfo.list.array[i]->present;
					switch (present)
					{
					case SystemInformation_r8_IEs__sib_TypeAndInfo__Member_PR_sib2: //sib2
					{
						printf("this is a sib2\n");
						fill_sib2_context(&(R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformation.criticalExtensions.choice.systemInformation_r8.sib_TypeAndInfo.list.array[i]->choice.sib2));


					}
						break;
					case SystemInformation_r8_IEs__sib_TypeAndInfo__Member_PR_sib3://sib3
					{
						printf("this is a sib3\n");
						fill_sib3_context(&(R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformation.criticalExtensions.choice.systemInformation_r8.sib_TypeAndInfo.list.array[i]->choice.sib3));
					}
						break;
					case SystemInformation_r8_IEs__sib_TypeAndInfo__Member_PR_sib5://sib5
					{
						printf("this is a sib5\n");
						fill_sib5_context(&(R12_BCCH_DL_SCH_Message_Ptr->message.choice.c1.choice.systemInformation.criticalExtensions.choice.systemInformation_r8.sib_TypeAndInfo.list.array[i]->choice.sib5));

					}
						break;
					default:
						printf("maybe unknow sib type\n");
						break;
					}
				}
			}
		}
	}else
	{
		printf("BCCH_DL_SCH_Message decoder failed: %d \n",rval_e.code);
	}
	ASN_STRUCT_FREE(*asn_pdu_collection[1], R12_BCCH_DL_SCH_Message_Ptr);
	printf("****************************\n");
}

void fill_sib1_context(SystemInformationBlockType1_t* systemInformationBlockType1_Ptr)
{
	int i = 0;
	int j = 0;
	memset(&g_sib1,0,sizeof(sib1));
	g_sib1.plmnList.numberOfElements = systemInformationBlockType1_Ptr->cellAccessRelatedInfo.plmn_IdentityList.list.count;
	for(i = 0; i < g_sib1.plmnList.numberOfElements;i++)
	{
		for(j = 0;j < systemInformationBlockType1_Ptr->
		cellAccessRelatedInfo.plmn_IdentityList.list.array[i]->plmn_Identity.mcc->list.count;j++)
		{
			g_sib1.plmnList.elm[i].plmn_Identity.mcc.elm[j]
			   = *(systemInformationBlockType1_Ptr->cellAccessRelatedInfo.
			   plmn_IdentityList.list.array[i]->plmn_Identity.mcc->list.array[j]);
			printf("mcc i %d elm[%d] %d\n",i,j,g_sib1.plmnList.elm[i].plmn_Identity.mcc.elm[j]);
		}
		for(j = 0;j < systemInformationBlockType1_Ptr->cellAccessRelatedInfo.
		plmn_IdentityList.list.array[i]->plmn_Identity.mnc.list.count;j++)
		{
			g_sib1.plmnList.elm[i].plmn_Identity.mnc.elm[j]
				= *(systemInformationBlockType1_Ptr->cellAccessRelatedInfo.
				plmn_IdentityList.list.array[i]->plmn_Identity.mnc.list.array[j]);
			printf("mnc i %d elm[%d] %d\n",i,j,g_sib1.plmnList.elm[i].plmn_Identity.mnc.elm[j]);
		}
	}
	memcpy(g_sib1.trackingAreaCode.byteContext,
			systemInformationBlockType1_Ptr->cellAccessRelatedInfo.trackingAreaCode.buf,2);
	printf("trackingAreaCode:");

	for(i = 0; i <2;i++)
	{
		printf("%02x",g_sib1.trackingAreaCode.byteContext[i]);

	}
	printf("\n");

	memcpy(g_sib1.cellIdentity.byteContext,
			systemInformationBlockType1_Ptr->cellAccessRelatedInfo.cellIdentity.buf,4);
	printf("cellIdentity:");

	for(i = 0; i <4;i++)
	{
		printf("%02x",g_sib1.cellIdentity.byteContext[i]);

	}
	printf("\n");
	g_sib1.cellBarred = systemInformationBlockType1_Ptr->cellAccessRelatedInfo.cellBarred;
	g_sib1.intraFreqReselection = systemInformationBlockType1_Ptr->cellAccessRelatedInfo.intraFreqReselection;
	g_sib1.si_WindowLength = systemInformationBlockType1_Ptr->si_WindowLength;
	g_sib1.systemInfoValueTag = systemInformationBlockType1_Ptr->systemInfoValueTag;
	g_sib1.Q_RxLevMin = systemInformationBlockType1_Ptr->cellSelectionInfo.q_RxLevMin;
	for(i = 0;i < systemInformationBlockType1_Ptr->schedulingInfoList.list.count;i++)
	{
		g_sib1.schedulingInfoList.elem[i].si_Periodicity = systemInformationBlockType1_Ptr->schedulingInfoList.list.array[i]->si_Periodicity;
		for(j = 0;j < systemInformationBlockType1_Ptr->schedulingInfoList.list.array[i]->sib_MappingInfo.list.count;j++)
		{
			g_sib1.schedulingInfoList.elem[i].sib_MapInfo.elem[j] = *(systemInformationBlockType1_Ptr->schedulingInfoList.list.array[i]->sib_MappingInfo.list.array[j]);
			printf("schedulingInfoList elem[%d] sib_MapInfo.elem[%d ] %d \n",i,j,g_sib1.schedulingInfoList.elem[i].sib_MapInfo.elem[j]);
		}
		printf("elem[%d].si_Periodicity %d\n",i,g_sib1.schedulingInfoList.elem[i].si_Periodicity);
	}
}

void fill_sib2_context(SystemInformationBlockType2_t* SystemInformationBlockType2_Ptr)
{
	int i = 0;
	int j = 0;
	memset(&g_sib2,0,sizeof(sib2));
	if(NULL == SystemInformationBlockType2_Ptr)
	{
		printf("SystemInformationBlockType2_Ptr is NULL\n");
		fflush(stdout);
	}
	if(NULL != SystemInformationBlockType2_Ptr->ac_BarringInfo)
	{
		g_sib2.ac_BarringFor_Emergency = SystemInformationBlockType2_Ptr->ac_BarringInfo->ac_BarringForEmergency;
		printf("ac_BarringForEmergency %d \n",(int)g_sib2.ac_BarringFor_Emergency);
		fflush(stdout);

	}
	g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.prach_ConfigIndex
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.prach_Config.prach_ConfigInfo.prach_ConfigIndex;
	g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.highSpeedFlag
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.prach_Config.prach_ConfigInfo.highSpeedFlag;
	g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.zeroCorrelationZoneConfig
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.prach_Config.prach_ConfigInfo.zeroCorrelationZoneConfig;
	g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.prach_FreqOffset
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.prach_Config.prach_ConfigInfo.prach_FreqOffset;
	g_sib2.radioResource_ConfigCommon.prachConfig.rootSequenceIndex
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.prach_Config.rootSequenceIndex;
	printf("prach_Config prach_ConfigIndex %d highSpeedFlag %d zeroCorrelationZoneConfig %d prach_FreqOffset %d rootSequenceIndex %d\n",
			g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.prach_ConfigIndex,
			g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.highSpeedFlag,
			g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.zeroCorrelationZoneConfig,
			g_sib2.radioResource_ConfigCommon.prachConfig.prach_ConfigInfo.prach_FreqOffset,
			g_sib2.radioResource_ConfigCommon.prachConfig.rootSequenceIndex);


	g_sib2.radioResource_ConfigCommon.rach_config_Common.maxHARQ_Msg3Tx
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.maxHARQ_Msg3Tx;
	g_sib2.radioResource_ConfigCommon.rach_config_Common.powerRampingParameters.powerRampingStep
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.powerRampingParameters.powerRampingStep;
	g_sib2.radioResource_ConfigCommon.rach_config_Common.powerRampingParameters.preambleInitialReceivedTargetPower
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.powerRampingParameters.preambleInitialReceivedTargetPower;

	printf("rach_config_Common:maxHARQ_Msg3Tx %d powerRampingStep %d Power %d\n",
			g_sib2.radioResource_ConfigCommon.rach_config_Common.maxHARQ_Msg3Tx,
			g_sib2.radioResource_ConfigCommon.rach_config_Common.powerRampingParameters.powerRampingStep,
			g_sib2.radioResource_ConfigCommon.rach_config_Common.powerRampingParameters.preambleInitialReceivedTargetPower);

	g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.numberOfRA_Preambles
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.preambleInfo.numberOfRA_Preambles;

	if(NULL != SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.preambleInfo.preamblesGroupAConfig)
	{
		g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.m.preamblesGroupAConfigPresent = 1;
		g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.messagePowerOffsetGroupB
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.preambleInfo.preamblesGroupAConfig->messagePowerOffsetGroupB;
		g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.messageSizeGroupA
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.preambleInfo.preamblesGroupAConfig->messageSizeGroupA;
		g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.sizeOfRA_PreamblesGroupA
			= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
	}
	printf("preambleInfo:numberOfRA_Preambles %d messagePowerOffsetGroupB %d messageSizeGroupA %d sizeOfRA_PreamblesGroupA %d\n",
			g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.numberOfRA_Preambles,
			g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.messagePowerOffsetGroupB,
			g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.messageSizeGroupA,
			g_sib2.radioResource_ConfigCommon.rach_config_Common.preambleInfo.preamblesGroupAConfig.sizeOfRA_PreamblesGroupA);

	g_sib2.radioResource_ConfigCommon.rach_config_Common.ra_SupervisionInfo.mac_ContentionResolutionTimer
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.ra_SupervisionInfo.mac_ContentionResolutionTimer;
	g_sib2.radioResource_ConfigCommon.rach_config_Common.ra_SupervisionInfo.preambleTransMax
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.ra_SupervisionInfo.preambleTransMax;
	g_sib2.radioResource_ConfigCommon.rach_config_Common.ra_SupervisionInfo.ra_ResponseWindowSize
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.rach_ConfigCommon.ra_SupervisionInfo.ra_ResponseWindowSize;

	g_sib2.radioResource_ConfigCommon.bcchConfig = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.bcch_Config.modificationPeriodCoeff;

	g_sib2.radioResource_ConfigCommon.pcchConfig.defaultPagingCycle = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pcch_Config.defaultPagingCycle;
	g_sib2.radioResource_ConfigCommon.pcchConfig.nB = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pcch_Config.nB;

	g_sib2.radioResource_ConfigCommon.pdschConfigCommon.p_b = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pdsch_ConfigCommon.p_b;
	g_sib2.radioResource_ConfigCommon.pdschConfigCommon.referenceSignalPower = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pdsch_ConfigCommon.referenceSignalPower;

	g_sib2.radioResource_ConfigCommon.pucchConfigCommon.deltaPUCCH_Shift = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pucch_ConfigCommon.deltaPUCCH_Shift;
	g_sib2.radioResource_ConfigCommon.pucchConfigCommon.n1PUCCH_AN = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pucch_ConfigCommon.n1PUCCH_AN;
	g_sib2.radioResource_ConfigCommon.pucchConfigCommon.nCS_AN = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pucch_ConfigCommon.nCS_AN;
	g_sib2.radioResource_ConfigCommon.pucchConfigCommon.nRB_CQI = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pucch_ConfigCommon.nRB_CQI;

	g_sib2.radioResource_ConfigCommon.puschConfigCommon.pusch_ConfigBasic.enable64QAM
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.pusch_ConfigBasic.enable64QAM;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.pusch_ConfigBasic.hoppingMode
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.pusch_ConfigBasic.hoppingMode;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.pusch_ConfigBasic.n_SB
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.pusch_ConfigBasic.n_SB;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled;
	g_sib2.radioResource_ConfigCommon.puschConfigCommon.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled;

	g_sib2.radioResource_ConfigCommon.ul_CyclicPrefix_Length = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.ul_CyclicPrefixLength;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.alpha = SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.alpha;

	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format1
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format1;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format1b
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format1b;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2a
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2a;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2b
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaFList_PUCCH.deltaF_PUCCH_Format2b;

	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.deltaPreambleMsg3
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.deltaPreambleMsg3;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.p0_NominalPUCCH
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.p0_NominalPUCCH;
	g_sib2.radioResource_ConfigCommon.uplinkPower_ControlCommon.p0_NominalPUSCH
		= SystemInformationBlockType2_Ptr->radioResourceConfigCommon.uplinkPowerControlCommon.p0_NominalPUSCH;
	g_sib2.freq_Info.additionalSpectrumEmission = SystemInformationBlockType2_Ptr->freqInfo.additionalSpectrumEmission;
	if(NULL != SystemInformationBlockType2_Ptr->freqInfo.ul_Bandwidth)
	{
		g_sib2.freq_Info.m.ul_BandwidthPresent = 1;
		g_sib2.freq_Info.ul_Bandwidth = *(SystemInformationBlockType2_Ptr->freqInfo.ul_Bandwidth);
		printf("ul_Bandwidth %d\n",g_sib2.freq_Info.ul_Bandwidth);
	}
	if(NULL != SystemInformationBlockType2_Ptr->freqInfo.ul_CarrierFreq)
	{
		g_sib2.freq_Info.m.ul_CarrierFreqPresent = 1;
		g_sib2.freq_Info.ul_CarrierFreq = *(SystemInformationBlockType2_Ptr->freqInfo.ul_CarrierFreq);
		printf("ul_CarrierFreq %d\n",g_sib2.freq_Info.ul_CarrierFreq);
	}
	g_sib2.timeAlignmentTimer_Common = SystemInformationBlockType2_Ptr->timeAlignmentTimerCommon;
	g_sib2.ue_TimersAnd_Constants.n310 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.n310;
	g_sib2.ue_TimersAnd_Constants.n311 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.n311;
	g_sib2.ue_TimersAnd_Constants.t300 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.t300;
	g_sib2.ue_TimersAnd_Constants.t301 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.t301;
	g_sib2.ue_TimersAnd_Constants.t310 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.t310;
	g_sib2.ue_TimersAnd_Constants.t311 = SystemInformationBlockType2_Ptr->ue_TimersAndConstants.t311;

}
void fill_sib3_context(SystemInformationBlockType3_t* SystemInformationBlockType3_Ptr)
{
	int i = 0;
	int j = 0;
	memset(&g_sib3,0,sizeof(sib3));
	g_sib3.cellReselectionInfoCommon.q_Hyst = SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.q_Hyst;
	if(NULL != SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars)
	{
		g_sib3.cellReselectionInfoCommon.m.speedStateReselectionParsPresent = 1;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.mobilityStateParameters.n_CellChangeHigh
			= SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.n_CellChangeHigh;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.mobilityStateParameters.n_CellChangeMedium
			= SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.n_CellChangeMedium;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.mobilityStateParameters.t_Evaluation
			= SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.t_Evaluation;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.mobilityStateParameters.t_HystNormal
			= SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.t_HystNormal;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.q_HystSF.sf_High
			 =SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->q_HystSF.sf_High;
		g_sib3.cellReselectionInfoCommon.SpeedStateReselectionPars.q_HystSF.sf_Medium
			 =SystemInformationBlockType3_Ptr->cellReselectionInfoCommon.speedStateReselectionPars->q_HystSF.sf_Medium;
	}

	g_sib3.cellReselectionServingFreqInfo.cellReselectionPriority = SystemInformationBlockType3_Ptr->cellReselectionServingFreqInfo.cellReselectionPriority;
	printf("sib3_cellReselectionPriority %d\n",g_sib3.cellReselectionServingFreqInfo.cellReselectionPriority);
	g_sib3.cellReselectionServingFreqInfo.threshServingLow = SystemInformationBlockType3_Ptr->cellReselectionServingFreqInfo.threshServingLow;
	if(NULL != SystemInformationBlockType3_Ptr->cellReselectionServingFreqInfo.s_NonIntraSearch)
	{
		g_sib3.cellReselectionServingFreqInfo.m.s_NonIntraSearchPresent = 1;
		g_sib3.cellReselectionServingFreqInfo.s_NonIntraSearch = *(SystemInformationBlockType3_Ptr->cellReselectionServingFreqInfo.s_NonIntraSearch);
		printf("s_NonIntraSearch %d \n",g_sib3.cellReselectionServingFreqInfo.s_NonIntraSearch);
	}

	g_sib3.intraFreqCellReselectionInfo.presenceAntennaPort1 = SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.presenceAntennaPort1;
	g_sib3.intraFreqCellReselectionInfo.neighCellConfig.numbits = SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.neighCellConfig.size;
	memcpy(&(g_sib3.intraFreqCellReselectionInfo.neighCellConfig.data),SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.neighCellConfig.buf,
			g_sib3.intraFreqCellReselectionInfo.neighCellConfig.numbits); //BIT STRING (SIZE (2))
	for(i = 0;i < g_sib3.intraFreqCellReselectionInfo.neighCellConfig.numbits;i++)
	{
		printf("neighCellConfig.data[%d]: %d\n",i,g_sib3.intraFreqCellReselectionInfo.neighCellConfig.data[i]);
	}
	g_sib3.intraFreqCellReselectionInfo.t_ReselectionEUTRA = SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.t_ReselectionEUTRA;

	if(NULL != SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.allowedMeasBandwidth)
	{
		g_sib3.intraFreqCellReselectionInfo.m.allowedMeasBandwidthPresent = 1;
		g_sib3.intraFreqCellReselectionInfo.allowedMeasBandwidth = *(SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.allowedMeasBandwidth);
	}
	if(NULL != SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.p_Max)
	{
		g_sib3.intraFreqCellReselectionInfo.m.p_MaxPresent = 1;
		g_sib3.intraFreqCellReselectionInfo.p_Max = *(SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.p_Max);
	}
	if(NULL != SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.s_IntraSearch)
	{
		g_sib3.intraFreqCellReselectionInfo.m.s_IntraSearchPresent = 1;
		g_sib3.intraFreqCellReselectionInfo.s_IntraSearch = *(SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.s_IntraSearch);
	}
	if(NULL != SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.t_ReselectionEUTRA_SF)
	{
		g_sib3.intraFreqCellReselectionInfo.m.t_ReselectionEUTRA_SFPresent = 1;
		g_sib3.intraFreqCellReselectionInfo.t_ReselectionEUTRA_SF.sf_High = SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.t_ReselectionEUTRA_SF->sf_High;
		g_sib3.intraFreqCellReselectionInfo.t_ReselectionEUTRA_SF.sf_Medium = SystemInformationBlockType3_Ptr->intraFreqCellReselectionInfo.t_ReselectionEUTRA_SF->sf_Medium;
	}

	if(NULL != SystemInformationBlockType3_Ptr->lateNonCriticalExtension)
	{
		g_sib3.m.lateNonCriticalExtensionPresent =1;
		g_sib3.lateNonCriticalExtension.numocts = SystemInformationBlockType3_Ptr->lateNonCriticalExtension->size;
		memcpy(&(g_sib3.lateNonCriticalExtension.data),SystemInformationBlockType3_Ptr->lateNonCriticalExtension->buf,
				g_sib3.lateNonCriticalExtension.numocts);
	}


	if(NULL != SystemInformationBlockType3_Ptr->q_QualMin_r9)
	{
		g_sib3.m.q_QualMin_r9Present = 1;
		g_sib3.q_QualMin_r9 = *(SystemInformationBlockType3_Ptr->q_QualMin_r9);
		printf("q_QualMin_r9 %d\n",g_sib3.q_QualMin_r9);
	}

	if(NULL != SystemInformationBlockType3_Ptr->s_IntraSearch_v920)
	{
		g_sib3.m.s_IntraSearch_v920Present = 1;
		g_sib3.s_IntraSearch_v920.s_IntraSearchP_r9 = SystemInformationBlockType3_Ptr->s_IntraSearch_v920->s_IntraSearchP_r9;
		g_sib3.s_IntraSearch_v920.s_IntraSearchQ_r9 = SystemInformationBlockType3_Ptr->s_IntraSearch_v920->s_IntraSearchQ_r9;
		printf("s_IntraSearchP_r9 %d s_IntraSearchQ_r9 %d\n",g_sib3.s_IntraSearch_v920.s_IntraSearchP_r9,g_sib3.s_IntraSearch_v920.s_IntraSearchQ_r9);
	}
	if(NULL != SystemInformationBlockType3_Ptr->s_NonIntraSearch_v920)
	{
		g_sib3.m.s_NonIntraSearch_v920Present = 1;
		g_sib3.s_NonIntraSearch_v920.s_NonIntraSearchP_r9 = SystemInformationBlockType3_Ptr->s_NonIntraSearch_v920->s_NonIntraSearchP_r9;
		g_sib3.s_NonIntraSearch_v920.s_NonIntraSearchQ_r9 = SystemInformationBlockType3_Ptr->s_NonIntraSearch_v920->s_NonIntraSearchQ_r9;
		printf("s_NonIntraSearchP_r9 %d s_NonIntraSearchQ_r9 %d\n",g_sib3.s_NonIntraSearch_v920.s_NonIntraSearchP_r9,
				g_sib3.s_NonIntraSearch_v920.s_NonIntraSearchQ_r9);
	}
	if(NULL != SystemInformationBlockType3_Ptr->threshServingLowQ_r9)
	{
		g_sib3.m.threshServingLowQ_r9Present = 1;
		g_sib3.threshServingLowQ_r9 = *(SystemInformationBlockType3_Ptr->threshServingLowQ_r9);
		printf("threshServingLowQ_r9 %d\n",g_sib3.threshServingLowQ_r9);
	}
}
void fill_sib5_context(SystemInformationBlockType5_t* SystemInformationBlockType5_Ptr)
{
	int i = 0;
	int j = 0;
	memset(&g_sib5,0,sizeof(sib5));

	if(NULL != SystemInformationBlockType5_Ptr->lateNonCriticalExtension)
	{
		g_sib5.m.lateNonCriticalExtensionPresent = 1;
		g_sib5.lateNonCriticalExtension.numocts = SystemInformationBlockType5_Ptr->lateNonCriticalExtension->size;
		memcpy(&(g_sib5.lateNonCriticalExtension.data),SystemInformationBlockType5_Ptr->lateNonCriticalExtension->buf,
				g_sib5.lateNonCriticalExtension.numocts);
	}
	g_sib5.interFreqCarrierFreqList.n = SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.count;
	printf("sib5_interFreqCarrierFreqList n %d\n",g_sib5.interFreqCarrierFreqList.n);
	for(i = 0;i < g_sib5.interFreqCarrierFreqList.n;i++)
	{
		g_sib5.interFreqCarrierFreqList.elem[i].allowedMeasBandwidth
			= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->allowedMeasBandwidth;
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->cellReselectionPriority)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.cellReselectionPriorityPresent = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].cellReselectionPriority
				= *(SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->cellReselectionPriority);
			printf("sib5_list[%d]:cellReselectionPriority %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].cellReselectionPriority);
		}
		g_sib5.interFreqCarrierFreqList.elem[i].dl_CarrierFreq
			= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->dl_CarrierFreq;
		printf("sib5_list[%d]:dl_CarrierFreq %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].dl_CarrierFreq);
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqBlackCellList)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.interFreqBlackCellListPresent = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].interFreqBlackCellList.n
				= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqBlackCellList->list.count;
			for(j = 0;j < g_sib5.interFreqCarrierFreqList.elem[i].interFreqBlackCellList.n;j++)
			{
				g_sib5.interFreqCarrierFreqList.elem[i].interFreqBlackCellList.elem[j].start =
						SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqBlackCellList->list.array[j]->start;
				if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqBlackCellList->list.array[j]->range)
				{
					g_sib5.interFreqCarrierFreqList.elem[i].interFreqBlackCellList.elem[j].m.rangePresent = 1;
					g_sib5.interFreqCarrierFreqList.elem[i].interFreqBlackCellList.elem[j].range
						= *(SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqBlackCellList->list.array[j]->range);
				}
			}
		}
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqNeighCellList)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.interFreqNeighCellListPresent = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].interFreqNeighCellList.n =
					SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqNeighCellList->list.count;
			for(j = 0;j < g_sib5.interFreqCarrierFreqList.elem[i].interFreqNeighCellList.n;j++)
			{
				g_sib5.interFreqCarrierFreqList.elem[i].interFreqNeighCellList.elem[j].physCellId
					= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqNeighCellList->list.array[j]->physCellId;
				g_sib5.interFreqCarrierFreqList.elem[i].interFreqNeighCellList.elem[j].q_OffsetCell
					= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->interFreqNeighCellList->list.array[j]->q_OffsetCell;
			}
		}
		g_sib5.interFreqCarrierFreqList.elem[i].neighCellConfig.numbits
			= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->neighCellConfig.size;
		memcpy(&(g_sib5.interFreqCarrierFreqList.elem[i].neighCellConfig.data),SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->neighCellConfig.buf,
				g_sib5.interFreqCarrierFreqList.elem[i].neighCellConfig.numbits); // BIT STRING (SIZE (2))
		for(j = 0;j < g_sib5.interFreqCarrierFreqList.elem[i].neighCellConfig.numbits;j++)
		{
			printf("sib5_list[%d] neighCellConfig.data[%d] %d\n",i,j,g_sib5.interFreqCarrierFreqList.elem[i].neighCellConfig.data[j]);
		}
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->p_Max)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.p_MaxPresent = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].p_Max = *(SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->p_Max);
		}
		g_sib5.interFreqCarrierFreqList.elem[i].presenceAntennaPort1
			= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->presenceAntennaPort1;
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->q_OffsetFreq)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].q_OffsetFreq = *(SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->q_OffsetFreq);
		}
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->q_QualMin_r9)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.q_QualMin_r9Present = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].q_QualMin_r9
				= *(SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->q_QualMin_r9);
		}
		g_sib5.interFreqCarrierFreqList.elem[i].q_RxLevMin = SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->q_RxLevMin;
		printf("sib5_list[%d]:q_RxLevMin %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].q_RxLevMin);
		g_sib5.interFreqCarrierFreqList.elem[i].t_ReselectionEUTRA = SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->t_ReselectionEUTRA;
		printf("sib5_list[%d]:t_ReselectionEUTRA %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].t_ReselectionEUTRA);
		if(NULL != SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->t_ReselectionEUTRA_SF)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.t_ReselectionEUTRA_SFPresent = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].t_ReselectionEUTRA_SF.sf_High
				= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->t_ReselectionEUTRA_SF->sf_High;
			g_sib5.interFreqCarrierFreqList.elem[i].t_ReselectionEUTRA_SF.sf_Medium
				= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->t_ReselectionEUTRA_SF->sf_Medium;
		}
		g_sib5.interFreqCarrierFreqList.elem[i].threshX_High = SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->threshX_High;
		printf("sib5_list[%d]:threshX_High %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].threshX_High);
		g_sib5.interFreqCarrierFreqList.elem[i].threshX_Low = SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->threshX_Low;
		printf("sib5_list[%d]:threshX_Low %d\n",i,g_sib5.interFreqCarrierFreqList.elem[i].threshX_Low);
		if(NULL !=  SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->threshX_Q_r9)
		{
			g_sib5.interFreqCarrierFreqList.elem[i].m.threshX_Q_r9Present = 1;
			g_sib5.interFreqCarrierFreqList.elem[i].threshX_Q_r9.threshX_HighQ_r9
				= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->threshX_Q_r9->threshX_HighQ_r9;
			g_sib5.interFreqCarrierFreqList.elem[i].threshX_Q_r9.threshX_LowQ_r9
				= SystemInformationBlockType5_Ptr->interFreqCarrierFreqList.list.array[i]->threshX_Q_r9->threshX_LowQ_r9;
		}
	}


}
